﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region Using Namespaces

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using NUnit.Framework;
using Toulr.Domains.Account;
using Toulr.Models;
using Toulr.Models.Account;
using Toulr.Services;
using Toulr.Util.Ioc;

#endregion

namespace Toulr.Test.Services
{
    // ×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
    //                                                  本用例用于模拟用户进行的相关操作
    //  ×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
    // 01. 未登录用户
    // 02. 用户注册 OK
    //  03. 用户登录 OK
    //  04. 用户密码修改 OK
    //  05. 用户基本信息修改 OK
    //  06. 用户基本信息查看 OK
    //  07. 用户好友信息查看\删除，确认、拒绝 OK
    //  08. 用户账户信息查看 OK
    //  09. 我的商铺(我关注的商铺、我作为店长的商铺） 
    //  10. 我的团购(发起到团购、参与的团购)  
    //  11. 我的购买记录  
    //  12. 我的推广记录  
    //  13. 短消息（发送消息，系统消息、朋友消息、陌生人消息、新消息邮件通知） OK 
    //  ××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××× */
    /// <summary>
    ///   测试用户登录相关服务接口是否正常工作
    /// </summary>
    /// Author: James Cheung
    /// MachineName: JAMESCHEUNG-PC
    /// DateTime: 2011/5/10 14:51
    [TestFixture]
    public class UserContextServiceFixture
    {
        #region Setup/Teardown

        [SetUp]
        public void Init()
        {
            //从容器内初始化接口的实例
            _userContextService = UnityHelper.GetInstance<IUserContextService>();
        }

        [TearDown]
        public void Dispose()
        {
        }

        #endregion

        private IUserContextService _userContextService;

        // 01. 未登录用户
        /*××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
         * 测试在不指定用户标识情况下获取信息 ,包括：
         * 
         *××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××*/

        /// <summary>
        ///   测试用户注册操作
        /// </summary>
        public void UserRegisterTest()
        {
            var registerModel = new RegisterModel {Email = "sinory@qq.com", Pwd = "sinory"};
            //测试注册用户，系统应提示注册成功并提示用户到邮箱激活账户
            ActionResult<bool> result = _userContextService.Register(registerModel);
            Assert.AreEqual(true, result.Result, "错误，此处应该提示用户注册成功！");
            //测试注册用户，当前用户在数据库中已经存在，系统应提示注册失败并返回邮件已注册提示信息
            result = _userContextService.Register(registerModel);
            Assert.AreEqual(false, result.Result, "错误，此处应该提示用户注册失败，输入的Email地址已经存在！");
            _userContextService.DeleteUser("sinory@qq.com");
        }

        /// <summary>
        ///   测试用户好友信息查看\删除
        /// </summary>
        [Test]
        public void FriendsTest()
        {
            var registerModel = new RegisterModel {Email = "sinory@qq.com", Pwd = "sinory"};
            _userContextService.Register(registerModel); //首先注册一个用户
            registerModel = new RegisterModel { Email = "tkli@metarnet.com", Pwd = "sinory" };
            _userContextService.Register(registerModel); //首先注册一个用户
            registerModel = new RegisterModel { Email = "hyyu@metarnet.com", Pwd = "sinory" };
            _userContextService.Register(registerModel); //首先注册一个用户
            //通过登录获取用户ID
            var loginModel = new LoginModel {Email = "sinory@qq.com", Password = "sinory"};
            ActionResult<User> loginUser1 = _userContextService.Login(loginModel);
            loginModel = new LoginModel { Email = "tkli@metarnet.com", Password = "sinory" };
            ActionResult<User> loginUser2 = _userContextService.Login(loginModel);
            loginModel = new LoginModel { Email = "hyyu@metarnet.com", Password = "sinory" };
            ActionResult<User> loginUser3 = _userContextService.Login(loginModel);
            _userContextService.AddFriend(loginUser1.Result.Uid, loginUser2.Result.Uid, "测试");
            _userContextService.AddFriend(loginUser2.Result.Uid, loginUser1.Result.Uid, "测试");
            _userContextService.AddFriend(loginUser3.Result.Uid, loginUser1.Result.Uid, "测试");
            ActionResult<List<Friends>> result = _userContextService.GetFriends(loginUser1.Result.Uid);
            IList<int> ids = new List<int>();
            foreach (var friendse in result.Result)
            {
                Debug.Print(friendse.OriginalUser.NickName + "=>" + friendse.TargetUser.NickName);
                ids.Add(friendse.Id);
            }
            _userContextService.RemoveFriends(ids);
            _userContextService.DeleteUser("sinory@qq.com");
            _userContextService.DeleteUser("tkli@metarnet.com");
            _userContextService.DeleteUser("hyyu@metarnet.com");
        }

        /// <summary>
        ///   测试获取手机验证码方法
        /// </summary>
        [Test]
        public void GenerationMobilePhoneVerfyCode()
        {
            ActionResult<String> result = _userContextService.SendVerifyCode("1232222222");
            Assert.AreEqual(result.Result.Length, 4);
        }

        ///<summary>
        ///  13. 短消息（发送消息，系统消息、朋友消息、陌生人消息）
        ///</summary>
        [Test]
        public void MessageTest()
        {
            var registerModel = new RegisterModel {Email = "sinory@qq.com", Pwd = "sinory"};
            _userContextService.Register(registerModel); //首先注册一个用户
            registerModel.Email = "sinory@msn.com";
            registerModel.Pwd = "sinory";
            _userContextService.Register(registerModel); //首先注册一个用户
            var loginModel = new LoginModel {Email = "sinory@msn.com", Password = "sinory"};
            //测试登录——此处应提示邮件未校验
            ActionResult<User> result1 = _userContextService.Login(loginModel);
            loginModel.Email = "sinory@qq.com";
            loginModel.Password = "sinory";
            ActionResult<User> result2 = _userContextService.Login(loginModel);
            Assert.NotNull(result1);
            Assert.NotNull(result2);
            //发送测试消息
            _userContextService.SendMessage("测试消息", result2.Result.Uid, result1.Result.Uid);
            _userContextService.SendMessage("测试消息", result2.Result.Uid, result1.Result.Uid);
            _userContextService.SendMessage("测试消息", result2.Result.Uid, result1.Result.Uid);
            _userContextService.SendMessage("测试消息", result2.Result.Uid, result1.Result.Uid);
            //模拟获取测试消息
            var msgRecieve = _userContextService.GetMessages(result1.Result.Uid); //获取接收到消息
            var msgSent = _userContextService.GetMessages(result1.Result.Uid); //获取接收到消息
            Assert.AreEqual(msgRecieve.Result.Count, msgSent.Result.Count);
            //首先删除发送到消息
            foreach (var messagese in msgSent.Result)
            {
                _userContextService.Repository.DbContext.Entry(messagese).State = EntityState.Deleted;
            }
            _userContextService.Repository.DbContext.SaveChanges();
            _userContextService.DeleteUser("sinory@qq.com");
            _userContextService.DeleteUser("sinory@msn.com");
        }

        /// <summary>
        ///   测试用户登录操作
        /// </summary>
        [Test]
        public void UserLoginTest()
        {
            var registerModel = new RegisterModel {Email = "sinory@qq.com", Pwd = "sinory"};
            _userContextService.Register(registerModel); //首先注册一个用户
            var loginModel = new LoginModel {Email = "sinory@qq.com", Password = "sinory"};
            //测试登录——此处应提示邮件未校验
            ActionResult<User> result = _userContextService.Login(loginModel);
            Assert.AreEqual(result.ResultCode, 1); //提示电子邮件地址未验证
            //执行电子邮件验证
            _userContextService.VerifyEmail("12%EC%A3%5D%18%0D%E9%9B%CB%A7%51%B2%93%31%F9%C4");
            //测试重新发送验证邮件
            _userContextService.SendVerifyMail(loginModel.Email,"http://www.toulr.com");
            //测试登录-此处应该正常登录
            result = _userContextService.Login(loginModel);
            Assert.AreEqual(result.ResultCode, 0); //提示电子邮件地址未验证
            _userContextService.DeleteUser(loginModel.Email); //删除临时注册的用户
        }

        [Test]
        public void VerifyMobilePhoneTest()
        {
            _userContextService.VerifyMobilePhone("", "");
        }

        /// <summary>
        ///   测试用户修改个人信息操作
        /// </summary>
        [Test]
        public void UserModifyTest()
        {
            //ActionResult<User> result = _userContextService.Login(_loginModel);
            //Assert.AreEqual(result.ResultCode, 0, "用户登录失败！！");
            //如果用户登录成功，更改获得的User对象的内容
        }

        /// <summary>
        ///   测试用户角色信息获取与修改
        /// </summary>
        [Test]
        public void UserRoleTest()
        {
            //测试用户角色对象获取、及权限关联
//            ActionResult<User> result = _userContextService.Login();
//            Assert.AreEqual(result.ResultCode, 0, "用户登录失败！！");
//            Assert.NotNull(result.Result.Role, "用户没有关联权限！");
//            Assert.NotNull(result.Result.Role.RolePermission, "角色没有关联配置权限");
        }
        /// <summary>
        /// 用户账户信息查看\订单信息查看
        /// </summary>
        [Test]
        public void BillsTest()
        {
            //首先录入测试数据
            //首先注册一个用户
            var registerModel = new RegisterModel { Email = "sinory@qq.com", Pwd = "sinory" };
            _userContextService.Register(registerModel); 
            var loginModel = new LoginModel { Email = "sinory@qq.com", Password = "sinory" };
            //模拟加入用户的消费记录
            

            //测试获取订单信息


            //清除测试数据
        }
    }
}